package com.bs.web.controller;

import com.bs.model.User;
import com.bs.service.IAuthService;
import com.bs.vo.LoginParamVo;
import com.bs.vo.LoginVo;
import com.bs.vo.PassParamVo;
import com.bs.vo.RestResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 说明：
 * <p>
 * 权限接口
 *
 * @author 琪
 * @since 2025/5/7 22:09
 */
@RestController
@RequestMapping("auth")
@Api(tags = "权限接口")
public class AuthController {

    @Autowired
    private IAuthService authService;

    /**
     * 注册用户
     */
    @PostMapping("register")
    @ApiOperation("注册用户")
    public RestResultVo<User> register(@RequestBody User user) {
        User register = authService.register(user);
        return RestResultVo.ok(register);
    }

    /**
     * 登录
     */
    @PostMapping("login")
    @ApiOperation("登录")
    public RestResultVo<LoginVo> login(@RequestBody LoginParamVo loginParamVo) {
        LoginVo login = authService.login(loginParamVo.getUsername(), loginParamVo.getPassword());
        return RestResultVo.ok(login);
    }

    /**
     * 登出
     */
    @PostMapping("logout")
    @ApiOperation("登出")
    public RestResultVo<Boolean> logout() {
        authService.logout();
        return RestResultVo.success();
    }

    /**
     * 登录
     */
    @PostMapping("changePass")
    @ApiOperation("修改密码")
    public RestResultVo<Boolean> changePass(@RequestBody PassParamVo passParamVo) {
        authService.changePass(passParamVo.getOldPass(), passParamVo.getNewPass());
        return RestResultVo.success();
    }

    /**
     * 获取用户信息
     */
    @PostMapping("checkLoginUser")
    @ApiOperation("获取登录信息")
    public RestResultVo<LoginVo> checkLoginUser() {
        LoginVo loginVo = authService.getLoginUser();
        return RestResultVo.ok(loginVo);
    }
}
